package interfaceDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import classes.Client;


public class ClientJDBCDAO implements ClientDAOInterface {

	private DataSource ds;
	
	public ClientJDBCDAO(){
		try{
			
		Context ctx = new InitialContext() ;
		ds = (DataSource) ctx.lookup("java:comp/env/jdbc/video");
		}
		catch(Exception e){
			System.out.println("exception constructeur DAO : " + e);
		}
	}
	
	@Override
	public ArrayList<Client> getClientList() {
		ArrayList<Client>clientsList = new ArrayList<Client>();
		Connection con = null;
		
		try{
		    con = ds.getConnection();
		    PreparedStatement pstmt=con.prepareStatement("SELECT ID,NOM,PRENOM,ADRESSE,TEL,EMAIL,ID_SESSION FROM CLIENT");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()){
				clientsList.add(new Client(rs.getLong(1),rs.getString(2),rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getLong(7)));
				}
			}
			catch(Exception e){
				System.out.println("erreur getClientList " + e);
			}
			finally{
				if (con != null){
					
					try{
						con.close();
					}
					catch(Exception e)	{
						System.out.println(e);
					}
				}
			}		
		return clientsList;	
	}
	
	@Override
	public Client getById(Long id) {
		Connection con = null;
		Client client = null;

		try{
	    con = ds.getConnection();		
	    PreparedStatement pstmt=con.prepareStatement("SELECT NOM,PRENOM,ADRESSE,TEL,EMAIL,ID_SESSION FROM CLIENT WHERE ID=?");
		pstmt.setLong(1, id);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()){
				client = new Client(id, rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getLong(6));
			}
		}
		catch(Exception e){
			System.out.println("erreur getById  ClientDAO " + e);
		}
		finally{
			if (con != null){
				
				try{
					con.close();
				}
				catch(Exception e)	{
					System.out.println(e);
				}
			}
		}					
		return client;
	}

	@Override
	public void create(Client client) {
		Connection con = null;		
		try{
		    con = ds.getConnection();
			PreparedStatement pstmt = con.prepareStatement("INSERT INTO CLIENT (NOM,PRENOM,ADRESSE,TEL,EMAIL,ID_SESSION) values (?,?,?,?,?,?)",new String[]{"ID"});
			pstmt.setString(1, client.getNom());
			pstmt.setString(2, client.getPrenom());
			pstmt.setString(3, client.getAdresse());
			pstmt.setString(4, client.getTel());
			pstmt.setString(5, client.getEmail());
			pstmt.setLong(6, client.getSessionId());
			pstmt.executeUpdate();
			ResultSet rs = pstmt.getGeneratedKeys();
			if(rs.next())
				client.setId(rs.getLong(1));
		}
		
		catch(Exception e){
			System.out.println("Exception methode create de la DAO client : " + e);
		}
		finally{
			if (con != null){
				
				try{
					con.close();
				}
				catch(Exception e)	{
					System.out.println(e);
				}
			}
		}
	}

	@Override
	public void update(Client client) {
		Connection con = null;
		try{
			con = this.ds.getConnection();
			PreparedStatement pstmt = con.prepareStatement("UPDATE CLIENT SET NOM = ?, PRENOM = ?, ADRESSE = ?, TEL = ?, EMAIL = ?, ID_SESSION = ? WHERE ID = ?");
			pstmt.setString(1, client.getNom());
			pstmt.setString(2, client.getPrenom());
			pstmt.setString(3, client.getAdresse());
			pstmt.setString(4, client.getTel());
			pstmt.setString(5, client.getEmail());
			pstmt.setLong(6, client.getSessionId());
			pstmt.setLong(7, client.getId());
			pstmt.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			if(con != null){
				try{
					con.close();
				}
				catch(Exception e){
					System.out.println(e);
				}
			}
		}
		
	}

	@Override
	public void delete(Client client) {
		Connection con = null;	
		try{
	    con = ds.getConnection();		
		PreparedStatement pstmt = con.prepareStatement("DELETE CLIENT WHERE ID = ?");
		pstmt.setLong(1, client.getId());
		pstmt.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			if (con != null){
				
				try{
					con.close();
				}
				catch(Exception e)	{
					System.out.println(e);
				}
			}
		}	
	}

	@Override
	public Client getByFirstAndLastNames(String prenom, String nom) {
		Connection con = null;
		Client client = null;

		try{
	    con = ds.getConnection();		
	    PreparedStatement pstmt=con.prepareStatement("SELECT ID,ADRESSE,TEL,EMAIL,ID_SESSION FROM CLIENT WHERE NOM = ? AND PRENOM = ?");
		pstmt.setString(1, nom);
		pstmt.setString(2, prenom);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()){
				client = new Client(rs.getLong(1),nom, prenom, rs.getString(2),rs.getString(3),rs.getString(4),rs.getLong(5));
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			if (con != null){
				
				try{
					con.close();
				}
				catch(Exception e)	{
					System.out.println(e);
				}
			}
		}					
		return client;
	}		
}
